//package com.pzh.logistics.log;
//
//import com.pzh.logistics.common.constant.CookieConstant;
//import com.pzh.logistics.common.constant.RedisConstant;
//import com.pzh.logistics.util.CookieUtil;
//import lombok.extern.slf4j.Slf4j;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Pointcut;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//import org.springframework.web.multipart.MultipartFile;
//
//import javax.servlet.http.Cookie;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//
//@Aspect
//@Component
//@Slf4j
//public class LogPrintAspect {
//
//    @Autowired
//    private RedisService redisService;
//
//    @Autowired
//    private UserService userService;
//
//    @Autowired
//    private LogService logService;
//    /** 换行符 */
//    private static final String LINE_SEPARATOR = System.lineSeparator();
//
//    /** 以自定义 @LogPrint 注解为切点 */
//    @Pointcut("@annotation(com.jh.draw.log.LogPrint)")
//    public void logPrint() {}
//
//    /**
//     * 环绕切点。 proceedingJoinPoint.proceed()：被切入的方法
//     * @param proceedingJoinPoint
//     * @return
//     * @throws Throwable
//     */
//    @Around("logPrint()")
//    public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
//        //Before
//        // 开始打印请求日志
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = attributes.getRequest();
//
//        Cookie cookie = CookieUtil.get(request, CookieConstant.TOKEN);
//        String user = "未登录用户";
//        if (cookie != null) {
//            String key = String.format(RedisConstant.TOKEN_PREFIX, cookie.getValue());
//            String val_str = redisService.get(key);
//            Integer userId = Integer.valueOf(val_str);
//            user = userService.getUserInfoById(userId).getUsername();
//        }
//        LogItem logItem = new LogItem();
//
//        // 打印请求相关参数
//        log.info("========================================== Start ==========================================");
//        // 打印请求 url
//        log.info("URL            : {}", request.getRequestURL().toString());
//        // 打印 Http method
//        log.info("HTTP Method    : {}", request.getMethod());
//        // 打印调用 controller 的全路径以及执行方法
//        String classMethod = String.format("%s.%s", proceedingJoinPoint.getSignature().getDeclaringTypeName(), proceedingJoinPoint.getSignature().getName());
//        log.info("Class Method   : {}.{}", proceedingJoinPoint.getSignature().getDeclaringTypeName(), proceedingJoinPoint.getSignature().getName());
//        // 打印请求的 IP
//        log.info("IP             : {}", request.getRemoteAddr());
//        // 打印请求用户
//        log.info("User : {}", user);
//        // 打印请求入参
//        String reqArgs = getParams(proceedingJoinPoint);
//        log.info("Request Args   : {}", reqArgs);
//
//        logItem.setUrl(request.getRequestURL().toString());
//        logItem.setHttpMethod(request.getMethod());
//        logItem.setClassMethod(classMethod);
//        logItem.setIp(request.getRemoteAddr());
//        logItem.setUsername(user);
//        logItem.setReqArgs(reqArgs);
//
//        //Proceed
//        long startTime = System.currentTimeMillis();
//        Object result = proceedingJoinPoint.proceed();
//
//        // 打印出参
//        String resArgs = JSONObject.toJSONString(result);
//        log.info("Response Args  : {}",  resArgs);
//        // 执行耗时
//        Integer costTime = new Long(System.currentTimeMillis() - startTime).intValue();
//        log.info("Time-Consuming : {} ms", costTime);
////        logItem.setResArgs(resArgs);
//        logItem.setResArgs("");
//        logItem.setTimeConsume(costTime);
//        //After
//        log.info("=========================================== End ===========================================" + LINE_SEPARATOR);
//
//        logService.insertLog(logItem);
//        return result;
//    }
//
//    private String getParams(JoinPoint joinPoint) {
//        String params = "";
//        if (joinPoint.getArgs() != null && joinPoint.getArgs().length > 0) {
//            for (int i = 0; i < joinPoint.getArgs().length; i++) {
//                Object arg = joinPoint.getArgs()[i];
//                if ((arg instanceof HttpServletResponse) || (arg instanceof HttpServletRequest)
//                        || (arg instanceof MultipartFile) || (arg instanceof MultipartFile[])) {
//                    continue;
//                }
//                try {
//                    params += JSONObject.toJSONString(joinPoint.getArgs()[i]);
//                } catch (Exception e1) {
//                    log.error(e1.getMessage());
//                }
//            }
//        }
//        return params;
//    }
//
//}